package com.huike.web.controller.system;

import com.huike.clues.domain.SysMenu;
import com.huike.clues.domain.dto.CreateMenuDTO;
import com.huike.clues.result.RoleMenuTreeAjaxResult;
import com.huike.clues.service.ISysMenuService;
import com.huike.common.constant.ErrorMessageConstants;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysMenuDTO;
import com.huike.common.core.domain.model.LoginUser;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 菜单管理相关接口
 * @Author lph
 * @Date 2023-10-15
 */
@Api(tags = "菜单管理相关接口")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/menu")
public class SysMenuController {

    private final ISysMenuService iSysMenuService;

    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    @ApiOperation("列表展示")
    public AjaxResult<List<CreateMenuDTO>> getMenu(String menuName, String status) {
        log.info("Get menuName: {}, status: {}", menuName, status);
        return AjaxResult.success(iSysMenuService.getMenuList(menuName, status));
    }

    /**
     * 新增菜单
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜单")
    public AjaxResult addMenu(@RequestBody CreateMenuDTO dto) {
        log.info("Get CreateMenuDTO: {}", dto);
        if (0 == dto.getIsFrame()) {
            checkIfFrameStartWithHttp(dto.getPath());
        }
        iSysMenuService.addMenu(dto);
        return AjaxResult.success();
    }

    /**
     * 判断外链菜单是否以http://或https://开头
     *
     * @param path 路径
     */
    private void checkIfFrameStartWithHttp(String path) {
        if (!(path.startsWith("http://") || path.startsWith("https://"))) {
            throw new BaseException(ErrorMessageConstants.MENU_PATH_FORMAT_ERROR);
        }
    }

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @DeleteMapping("/{menuId}")
    @ApiOperation("删除菜单")
    public AjaxResult deleteMenu(@PathVariable Long menuId) {
        iSysMenuService.deleteMenu(menuId);
        return AjaxResult.success();
    }

    /**
     * 修改菜单回显数据
     *
     * @param menuId
     * @return
     */
    @GetMapping("/{menuId}")
    @ApiOperation("修改菜单回显数据")
    public AjaxResult<SysMenuDTO> updateByMenu(@PathVariable Long menuId) {
        SysMenuDTO sysMenuDTOList = iSysMenuService.updateByMenu(menuId);
        return AjaxResult.success(sysMenuDTOList);
    }

    /**
     * 修改菜单
     */
    @PutMapping
    @ApiOperation("修改菜单")
    public AjaxResult update(@RequestBody CreateMenuDTO dto) {
        log.info("Get dto: {}", dto);
        if (dto.getIsFrame() == 0) {
            checkIfFrameStartWithHttp(dto.getPath());
        }
        iSysMenuService.updateMenu(dto);
        return AjaxResult.success();
    }

    /**
     * 加载对应角色菜单列表树
     *
     * @param roleId 角色id
     */
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public RoleMenuTreeAjaxResult roleMenuTree(@PathVariable Long roleId) {
        log.info("Get roleId: {}", roleId);
        /* RoleMenuTreeAjaxResult res = RoleMenuTreeAjaxResult.success();
        res.setCheckedKeys(iSysMenuService.getMenuListByRoleId(SysMenu.builder().build(), roleId));
        List<SysMenu> menus = iSysMenuService.getMenuListByUserId(SecurityUtils.getUserId());
        res.setMenus(iSysMenuService.getRoleMenuTree(menus));
        return res; */
        return iSysMenuService.getRoleMenuTree(roleId);
    }

    @GetMapping("/treeselect")
    public AjaxResult<List<TreeSelect>> treeselect() {
        return AjaxResult.success(iSysMenuService.getTreeSelect());
    }

}
